package com.gsafety.wellsfort.controller.file;

import com.alibaba.fastjson.JSONObject;
import com.gsafety.wellsfort.annotation.CheckToken;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.HttpClientUtil;
import com.gsafety.wellsfort.common.util.OSSClientUtil;
import com.gsafety.wellsfort.common.util.StringUtil;
import com.gsafety.wellsfort.domain.dto.ids.PreviewFileDTO;
import com.gsafety.wellsfort.service.common.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "文件上传Controller", description = "文件上传Controller")
@RestController
@Slf4j
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "上传")
    @CheckToken(value = false)
    @PostMapping("/upload")
    public ApiResponse upload(MultipartFile multipartFile) throws Exception {
        String url = OSSClientUtil.uploadFile(multipartFile, false);
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isNotEmpty(url)) {
            jsonObject.put("url", url);
            return ApiResponse.success(jsonObject);
        }
        return ApiResponse.error();
    }

    @ApiOperation(value = "获取文件预览")
    @CheckToken(value = false)
    @PostMapping("/getPreviewFile")
    public ApiResponse getPreviewFile(@RequestBody PreviewFileDTO previewFileDTO) {
        return fileService.getPreviewFile(previewFileDTO);
    }

    @ApiOperation(value = "token过期,刷新文件预览")
    @CheckToken(value = false)
    @PostMapping("/refreshPreviewFile")
    public ApiResponse refreshPreviewFile(@RequestBody PreviewFileDTO previewFileDTO) {
        return fileService.refreshPreviewFile(previewFileDTO);
    }
}
